package com.nl.controller.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.BlackUser;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.service.BlackUserService;
import com.nl.controller.web.util.FileUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("blackUser")
public class BlackUserController {
    @Autowired
    private BlackUserService blackUserService;


    @ApiOperation(value = "黑名单添加", notes = "黑名单添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "投诉电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "地市编码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "enterTime", value = "投诉时间", required = true, dataType = "String")

    })
    @PostMapping("add")
    private @ResponseBody
    Response addBlackUser(
            @RequestParam(value = "phoneNumber", required = true) String phoneNumber,
            @RequestParam(value = "numberType", required = true) String numberType,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "countyId", required = false) String countyId,
            @RequestParam(value = "mgrId", required = false) String mgrId,
            @RequestParam(value = "blackLevel", required = false) String blackLevel,
            @RequestParam(value = "enterTime", required = false) String enterTime,
            @RequestParam(value = "operatorId", required = false) String operatorId,
            @RequestParam Map<String, Object> paramMap
    ) {
    	cityId = cityId == null?"":cityId;
    	countyId = countyId == null?"":countyId;
    	mgrId = mgrId == null?"":mgrId;
        //cityId = ("".equals(cityId) ? "25" : cityId);
        //cityId = ("10".equals(cityId) ? "25" : cityId);
        int result = blackUserService.addBlackUser(phoneNumber,numberType, cityId, countyId, mgrId, blackLevel, enterTime,operatorId);
        log.info(" 黑名单添加：{}", paramMap.toString());
        log.debug("查询黑名单添加返回参数：{}", result);
        return ResponseUtil.sucess(result);
    }

    @ApiOperation(value = "查询黑名单", notes = "分页查询黑名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "地市编码", required = true, dataType = "String"),
    })
    @PostMapping("find")
    public Response findBlackUser(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "phoneNumber", required = false) String phoneNumber,
            @RequestParam(value = "numberType", required = false) String numberType,
            @RequestParam(value = "countyId", required = false) String countyId,
            @RequestParam(value = "mgrId", required = false) String mgrId,
            @RequestParam(value = "blackLevel", required = false) String blackLevel,
            @RequestParam(value = "mgrName", required = false) String mgrName,
            @RequestParam(value = "duty", required = false) String duty,
            @RequestParam Map<String, Object> paramMap
    ) {
    	if ("".equals(duty) || duty == null) {
            throw new SmsException("请选择查询条件，开始查询");
        }else {
    	    log.info("----"+phoneNumber);
        	PageInfo<BlackUser> pageInfo = blackUserService.find(page, limit, cityId, phoneNumber,numberType,countyId,mgrId,blackLevel,mgrName);
            log.info(" 查询黑名单：{}", paramMap.toString());
            log.debug("查询黑名单返回参数：{}", pageInfo.getList().toString());
            return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
		}
    }

    @ApiOperation(value = "删除黑名单", notes = "删除黑名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "投诉电话", required = false, dataType = "String"),
    })
    @PostMapping("delete")
    public Response deleteBlackUser(
            @RequestParam(value = "phoneNumber") String phoneNumber,
            @RequestParam(value = "mgrId") String mgrId,
            @RequestParam(value = "cityId") String cityId,
            @RequestParam(value = "countyId") String countyId,
            @RequestParam(value = "blackLevel") String blackLevel
    ) {
        int result = 0;
        try {
            result = blackUserService.deleteBlackUser(phoneNumber,mgrId,cityId,countyId,blackLevel);
            log.info("删除黑名单:{}" + phoneNumber+","+mgrId);
            log.debug("删除黑名单返回信息：" + result);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("删除黑名单返回信息：");
        }
        return ResponseUtil.sucess(result);
    }
    
    @PostMapping("qryBlackCount")
    public Response qryBlackCount(
            @RequestParam(value = "operatorId") String operatorId
    ) {    	
    	int result = 0;
        try {
            result = blackUserService.qryBlackCount(operatorId);
            log.info("查询当天已录入的黑名单数量:{}" + operatorId);
            log.debug("查询当天已录入的黑名单数量：" + result);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("查询当天已录入的黑名单数量：");
        }
        return ResponseUtil.sucess(result);
    }
    
  //处理文件上传
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Response uploadImg(@RequestParam("file") MultipartFile file,
                                         HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();
        SmsUsers user = (SmsUsers) request.getSession().getAttribute("user");
        String now = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String fileName = user.getUserId() + now ;
        String path = GlobalConst.FILE_STATION_PATH;
        try {
            FileUtil.fileUpload(file,request,path,fileName); //文件上传
            log.info("文件上传成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error("文件上传异常");
            return ResponseUtil.error(1, "文件上传异常");
        }
            try {
                List list = blackUserService.readCsv(path + "/" + fileName); //文件解析
                String duty=user.getDuty();
                int size=list.size();
                if("1".equals(duty)&&size>5){               	
                	return ResponseUtil.error(1, "已超出上传上限,上限数量为 5 条,请重新上传");
                }else{
                	return ResponseUtil.sucess(list);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code", 1);
                log.error("数据异常请检查");
                return ResponseUtil.error(1, "数据异常请检查");
            }
        }
    @RequestMapping(value = "/modeldownload", method = RequestMethod.GET)
    public void download(HttpServletResponse res) throws Exception {
        String fileName = "msisdn.csv";
        FileUtil.download(fileName,res);
    }

    @RequestMapping(value = "/apidownload", method = RequestMethod.GET)
    public void download2(HttpServletResponse res) throws Exception {
        String fileName = "phoneNumberImportApi.docx";
        FileUtil.download(fileName,res);
    }
}
